#include "color.h"
#include "ui_color.h"
#include <fstream>
#include <iostream>
#include <QColorDialog>
#include <QScrollBar>
#include <QFontDialog>
#include "highlight.h"
using namespace std;

Color::Color(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Color)
{
    ui->setupUi(this);
    high = new highlight(ui->preview->fontPointSize(),ui->preview->document());
    std::ifstream f("highlight.gh");
    getline(f,table);
    std::string temp;
    getline(f,temp);
    labelColor = QString::fromStdString(temp).split('/')[0].toStdString();
    labelFamily = QString::fromStdString(temp).split('/')[1].toStdString();
    getline(f,Title);
    getline(f,Quote);
    getline(f,List);
    getline(f,Olist);
    getline(f,Link);
    getline(f,img);
    getline(f,em);
    getline(f,strong);
    getline(f,temp);
    codeColor = QString::fromStdString(temp).split('/')[0].toStdString();
    codeFamily = QString::fromStdString(temp).split('/')[1].toStdString();
    connect(ui->label,&QPushButton::pressed,this,&Color::setLabel);
    connect(ui->table,&QPushButton::pressed,this,&Color::setTable);
    connect(ui->em,&QPushButton::pressed,this,&Color::setem);
    connect(ui->strong,&QPushButton::pressed,this,&Color::setStrong);
    connect(ui->code,&QPushButton::pressed,this,&Color::setCode);
    connect(ui->link,&QPushButton::pressed,this,&Color::setLink);
    connect(ui->list,&QPushButton::pressed,this,&Color::setList);
    connect(ui->olist,&QPushButton::pressed,this,&Color::setOlist);
    connect(ui->img,&QPushButton::pressed,this,&Color::setimg);
    connect(ui->Title,&QPushButton::pressed,this,&Color::setTitle);
    connect(ui->quote,&QPushButton::pressed,this,&Color::setQuote);
    connect(ui->save,&QPushButton::pressed,this,&Color::save);
    connect(ui->view,&QPushButton::pressed,this,&Color::reload);
}

void Color::reload(){
    std::ofstream of("highlight.gh",std::ios::trunc);
    if(of.good()){
        of << (QString("%1\n%2/%3\n%4\n%5\n%6\n%7\n%8\n").arg(QString::fromStdString(table),QString::fromStdString(labelColor),QString::fromStdString(labelFamily),QString::fromStdString(Title),QString::fromStdString(Quote),QString::fromStdString(List),QString::fromStdString(Olist),QString::fromStdString(Link))).toStdString();
        of << QString("%1\n%2\n%3\n%4/%5\n").arg(QString::fromStdString(img),QString::fromStdString(em),QString::fromStdString(strong),QString::fromStdString(codeColor),QString::fromStdString(codeFamily)).toStdString();
    }else{
        return;
    }
    of.close();
    high->init(ui->preview->fontPointSize());
    int pos = ui->preview->verticalScrollBar()->value();
    ui->preview->setText(ui->preview->toPlainText());
    ui->preview->verticalScrollBar()->setValue(pos);
}

void Color::save(){
    std::ofstream of("highlight.gh",std::ios::trunc);
    if(of.good()){
        of << (QString("%1\n%2/%3\n%4\n%5\n%6\n%7\n%8\n").arg(QString::fromStdString(table),QString::fromStdString(labelColor),QString::fromStdString(labelFamily),QString::fromStdString(Title),QString::fromStdString(Quote),QString::fromStdString(List),QString::fromStdString(Olist),QString::fromStdString(Link))).toStdString();
        of << QString("%1\n%2\n%3\n%4/%5\n").arg(QString::fromStdString(img),QString::fromStdString(em),QString::fromStdString(strong),QString::fromStdString(codeColor),QString::fromStdString(codeFamily)).toStdString();
    }else{
        return;
    }
    close();
}

void Color::setLabel(){
    bool ok;
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(labelColor)),this,"Set Label Color.");
    if(temp.isValid()){
        labelColor = converRGB16HexStr(temp).toStdString();
    }
    QFont tempFont = QFontDialog::getFont(&ok,this);
    if(ok){
        labelFamily = tempFont.family().toStdString();
    }
    cout << labelFamily << endl;
}
void Color::setTable(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(table)),this,"Set Table Color.");
    if(temp.isValid()){
        table = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setTitle(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(Title)),this,"Set Title Color.");
    if(temp.isValid()){
        Title = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setQuote(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(Quote)),this,"Set Quote Color.");
    if(temp.isValid()){
        Quote = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setList(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(List)),this,"Set unordered list Color.");
    if(temp.isValid()){
        List = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setOlist(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(Olist)),this,"Set ordered list Color.");
    if(temp.isValid()){
        Olist = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setLink(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(Link)),this,"Set Link Color.");
    if(temp.isValid()){
        Link = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setimg(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(img)),this,"Set img Color.");
    if(temp.isValid()){
        img = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setem(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(em)),this,"Set emphasis Color.");
    if(temp.isValid()){
        em = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setStrong(){
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(strong)),this,"Set strong Color.");
    if(temp.isValid()){
        strong = converRGB16HexStr(temp).toStdString();
    }
}
void Color::setCode(){
    bool ok;
    QColor temp = QColorDialog::getColor(QColor(QString::fromStdString(codeColor)),this,"Set code Color.");
    if(temp.isValid()){
        codeColor = converRGB16HexStr(temp).toStdString();
    }
    QFont tempFont = QFontDialog::getFont(&ok,this);
    if(ok){
        codeFamily = tempFont.family().toStdString();
    }
    cout << codeFamily << endl;
}

Color::~Color()
{
    delete ui;
}

